var waterfall = {
	init: function(){
		console.log(this);
		this.rowHeights = [];//第一行图片高度
		this.superDivID = document.querySelector("#superDivID");//根div
		this.baseDivClass = document.querySelectorAll(".baseDivClass");//图片
		this.setLoadPictureOnRow(this.superDivID, this.baseDivClass);

	},
	//得到数组中最小值index
	getMinIndex: function(minHeight){
		for (var i in this.rowHeights) {
			if (this.rowHeights[i] === minHeight) {
				return i;
			}
		}
	},
	setLoadPictureOnRow: function(superDivID, baseDivClass){
		var screenWidth = document.body.offsetWidth;
		var imgWidth = baseDivClass[0].offsetWidth;
		//计算一行显示多少图片
		var num = Math.floor(screenWidth/imgWidth);
		console.log(num)
		superDivID.style.cssText = "width:"+imgWidth*num+"px;margin:0 auto;"
		for (var i=0;i<baseDivClass.length;i++){
			var tmpDiv = baseDivClass[i];
			if (i < num) {
				this.rowHeights.push(tmpDiv.offsetHeight);
			} else {
				//计算数组中最小值
				var minHeight = Math.min.apply(this,this.rowHeights);
				//计算最小值的index
				var minIndex = this.getMinIndex(minHeight);
				//设置img的position
				tmpDiv.style.position = "absolute";
				tmpDiv.style.top = minHeight+"px";
				tmpDiv.style.left = baseDivClass[minIndex].offsetLeft + "px";
				//更新图片的列高度
				this.rowHeights[minIndex] += tmpDiv.offsetHeight;

			}
		}

	}
}

window.onload = function(){
	waterfall.init();
}






